<?php

namespace app\controllers;

use Yii;
use app\models\User;
use app\models\Article;
use app\models\Photos;
use app\models\Music;
use app\models\Mood;
use yii\web\Controller;
use yii\web\UploadedFile;
use yii\filters\AccessControl;

/**
 * CountryController implements the CRUD actions for country model.
 */
class AdminController extends Controller
{


    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => [ 'register','login'],
                'rules' => [

                    [
                        'allow' => true,
                        'roles' => ['?'],
                    ],



                ],
            ],
        ];
    }

    /**
     * Lists all country models.
     * @return mixed
     */
    public function actionView()
    {
        return $this->render(
            'login', [

        ]);

    }
    public function actionLogin()
    {

        $model = new User(['scenario' => 'login']);
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $identity = User::findOne(['username' => $model['username'],'password'=>md5($model['password'])]);
            if (  $identity!= null) {


                Yii::$app->user->login($identity);
                $session=Yii::$app->session;
                $session['model']=$model;
                return $this->redirect('http://localhost/yii/web/index.php?r=admin/main');


            } else  return $this->goHome();
        }
        else  return  $this->render('login', [
        'model' => $model,
            ]);
    }

    /**
     * Displays a single country model.
     * @param string $id
     * @return mixed
     */
    public function actionRegister()
    {
        $fileName = "";
        $model = new User(['scenario' => 'register']);
        $model->image = UploadedFile::getInstances($model, 'image');
        foreach ($model->image as $file) {
            $fileName = 'http://localhost/yii/upload/' . $file->baseName . '.' . $file->extension;
            $file->saveAs('D:/Program Files/apache/htdocs/yii/upload/' . $file->baseName . '.' . $file->extension);
        }
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $connection = \Yii::$app->db;
            $model['password']=md5($model->password);
            $command = $connection->createCommand('insert into USER(username,password,birth,sex,image,remark) Values(:1,:2,:3,:4,:5,:6)');
            $command = $command->bindValue(':1', $model['username']);
            $command = $command->bindValue(':2', $model['password']);
            $command = $command->bindValue(':3', $model['birth']);
            $command = $command->bindValue(':4', $model['sex']);
            $command = $command->bindValue(':5', $fileName);
            $command = $command->bindValue(':6', $model['remark']);

            $result = $command->execute();
            if ($result != 0) {
                return $this->goHome();
            } else  return $this->render('register', [
                'model' => $model,

            ]);
        }

        return $this->render('register', [
            'model' => $model,]);
    }

    public function actionMain()
    {

        $session = Yii::$app->session;
        $this->isGuest();
        return $this->render('main', [
            'model' => $session['model'],

        ]);


    }

    public function actionAddarticle()
    {
        $this->isGuest();
        $model = new Article(['scenario' => 'addarticle']);
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $connection = \Yii::$app->db;
            $command = $connection->createCommand('insert into article(title,type,context,time) Values(:1,:2,:3,:4)');
            $command = $command->bindValue(':1', $model['title']);
            $command = $command->bindValue(':2', $model['type']);
            $command = $command->bindValue(':3', $model['context']);
            $command = $command->bindValue(':4', $model['time'] = date("Y M D H:i:s", time() + 21600));
            $result = $command->execute();
            if ($result != 0) {
                return '1';
            } else return '0';

        }

        return $this->render('addarticle', [
            'model' => $model,

        ]);


    }

    public function actionAddphotos()
    {

        $this->isGuest();
        $model = new Photos(['scenario' => 'addphotos']);
        $model->photos = UploadedFile::getInstances($model, 'photos');
        foreach ($model->photos as $file) {
            $fileName = 'http://localhost/yii/upload/' . $file->baseName . '.' . $file->extension;
            $file->saveAs('D:/Program Files/apache/htdocs/yii/upload/' . $file->baseName . '.' . $file->extension);
        }
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $connection = \Yii::$app->db;
            $command = $connection->createCommand('insert into  photos(photos,describes)  Values(:1,:2)');
            $command = $command->bindValue(':1', $fileName);
            $command = $command->bindValue(':2', $model['describes']);

            $result = $command->execute();
            if ($result != 0) {
                return '1';
            } else return '0';

        }

        return $this->render('addphotos', [
            'model' => $model,

        ]);


    }

    public function actionAddmusic()
    {

        $this->isGuest();
        $model = new Music();
        $model->music = UploadedFile::getInstances($model, 'music');
        foreach ($model->music as $file) {
            $fileName = 'http://localhost/yii/upload/music/' . $file->baseName . '.' . $file->extension;
            $file->saveAs('D:/Program Files/apache/htdocs/yii/upload/music/' . $file->baseName . '.' . $file->extension);
            $name=$file->baseName.'.'.$file->extension;
        }
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $connection = \Yii::$app->db;
            $command = $connection->createCommand('insert into  music(name,music)  Values(:2,:1)');
            $command = $command->bindValue(':2',$name);
            $command = $command->bindValue(':1', $fileName);


            $result = $command->execute();
            if ($result != 0) {
                return '1';
            } else return '0';

        }

        return $this->render('addmusic', [
            'model' => $model,

        ]);


    }

    public function actionAddmood()
    {

        $this->isGuest();
        $model = new Mood(['scenario' => 'addmood']);

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $connection = \Yii::$app->db;
            $command = $connection->createCommand('insert into mood(context,time)  Values(:1,:2)');
            $command = $command->bindValue(':1', $model['context']);
            $command = $command->bindValue(':2', $model['time'] = date("Y M D H:i:s", time() + 21600));


            $result = $command->execute();
            if ($result != 0) {
                return '1';
            } else return '0';

        }

        return $this->render('addmood', [
            'model' => $model,

        ]);


    }

    public function actionArticlelist()

    {

        $this->isGuest();

        $connection = Yii::$app->db;
        $command = $connection->createCommand('select * from article');
        $result = $command->queryAll();
        return $this->render('articlelist', [
            'model' => $result
        ]);
    }


    public function actionDeletearticle($id)
    {

        $connection = Yii::$app->db;
        $command = $connection->createCommand('delete from article where id=:1');
        $command = $command->bindValue(':1', $id);
        $result = $command->execute();
        return $this->redirect('http://localhost/yii/web/index.php?r=admin/articlelist');

    }
    public function actionFindarticle($id)
    {
        $model = new Article();

        $this->isGuest();



       $connection = Yii::$app->db;
       $command = $connection->createCommand('select * from article where id=:1');
        $command = $command->bindValue(':1', $id);
        $result = $command->queryOne();
        $model->id=$result['id'];
        $model->context=$result['context'];
        $model->title=$result['title'];
        $model->type=$result['type'];
        return $this->render('updatearticle', [
            'model' => $model
        ]);



    }
    public function actionUpdatearticle()
    {
        $model = new Article(['scenario' => 'updatearticle']);

        $this->isGuest();


        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $connection = Yii::$app->db;
            $command = $connection->createCommand('update article set title=:1,context=:2,type=:3 where id=:4');
            $command = $command->bindValue(':1', $model['title']);
            $command = $command->bindValue(':2', $model['context']);
            $command = $command->bindValue(':3', $model['type']);
            $command = $command->bindValue(':4', $model['id']);
            $result = $command->execute();


            $result = $command->execute();
            if ($result != 0) {
                return $this->redirect('http://localhost/yii/web/index.php?r=admin/articlelist');
            } else return '0';

        }
    }
    public function actionMoodlist()

    {

        $this->isGuest();

        $connection = Yii::$app->db;
        $command = $connection->createCommand('select * from mood');
        $result = $command->queryAll();
        return $this->render('moodlist', [
            'model' => $result
        ]);
    }
    public function actionDeletemood($id)
{

    $connection = Yii::$app->db;
    $command = $connection->createCommand('delete from mood where id=:1');
    $command = $command->bindValue(':1', $id);
    $result = $command->execute();
    return $this->redirect('http://localhost/yii/web/index.php?r=admin/moodlist');

}
    public function actionFindmood($id)
    {
        $model = new Mood();

        $this->isGuest();



        $connection = Yii::$app->db;
        $command = $connection->createCommand('select * from mood where id=:1');
        $command = $command->bindValue(':1', $id);
        $result = $command->queryOne();
        $model->id=$result['id'];
        $model->context=$result['context'];
        return $this->render('updatemood', [
            'model' => $model
        ]);



    }
    public function actionUpdatemood()
    {
        $model=new Mood(['scenario' => 'updatemood']);

        $this->isGuest();


        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $connection = Yii::$app->db;
            $command = $connection->createCommand('update mood set context=:1,time=:3 where id=:2');

            $command = $command->bindValue(':1', $model['context']);

            $command = $command->bindValue(':2', $model['id']);
            $command = $command->bindValue(':3', $model['time'] = date("Y M D H:i:s", time() + 21600));
            $result = $command->execute();


            if ($result != 0) {
                return $this->redirect('http://localhost/yii/web/index.php?r=admin/moodlist');
            } else return '0';

        }


    }
    public function actionPhotoslist()

    {

        $this->isGuest();

        $connection = Yii::$app->db;
        $command = $connection->createCommand('select * from photos');
        $result = $command->queryAll();
        return $this->render('photoslist', [
            'model' => $result
        ]);
    }
    public function actionDeletephotos($id)
    {

        $this->isGuest();

        $connection = Yii::$app->db;
        $command = $connection->createCommand('delete from photos where id=:1');
        $command = $command->bindValue(':1', $id);
        $result = $command->execute();
        return $this->redirect('http://localhost/yii/web/index.php?r=admin/photoslist');

    }
    public function actionFindphotos($id)
    {
        $model = new Photos();

        $this->isGuest();



        $connection = Yii::$app->db;
        $command = $connection->createCommand('select * from photos where id=:1');
        $command = $command->bindValue(':1', $id);
        $result = $command->queryOne();
        $model->id=$result['id'];
        $model->photos=$result['photos'];
        $model->describes=$result['describes'];
        return $this->render('updatephotos', [
            'model' => $model
        ]);



    }
    public function actionUpdatephotos()
    {
        $model = new Photos(['scenario' => 'updatephotos']);

        $this->isGuest();


        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $connection = Yii::$app->db;
            $command = $connection->createCommand('update photos set describes=:2 where id=:3');


            $command = $command->bindValue(':2', $model['describes']);

            $command = $command->bindValue(':3', $model['id']);
            $result = $command->execute();


            if ($result != 0) {
                return $this->redirect('http://localhost/yii/web/index.php?r=admin/photoslist');
            } else return '0';

        }
    }
        public function actionMusiclist()

    {

        $this->isGuest();

        $connection = Yii::$app->db;
        $command = $connection->createCommand('select * from music');
        $result = $command->queryAll();
        return $this->render('musiclist', [
            'model' => $result
        ]);
    }
        public function actionDeletemusic($id)
    {

        $this->isGuest();

        $connection = Yii::$app->db;
        $command = $connection->createCommand('delete from music where id=:1');
        $command = $command->bindValue(':1', $id);
        $result = $command->execute();
        return $this->redirect('http://localhost/yii/web/index.php?r=admin/musicslist');

    }
    public function isGuest()
    {
        $session = Yii::$app->session;

        if($session['model']==null)
        {
            return $this->goHome();
        }

    }




}